package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.constant.TypeConstant;
import com.sky.context.BaseContext;
import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.exception.AddDataException;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.CategoryMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.CategoryService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;


@Service
public class CategoryServiceImpl implements CategoryService {
	
	@Autowired
	private CategoryMapper categoryMapper;
	@Autowired
	private DishMapper dishMapper;
	@Autowired
	private SetmealMapper setmealMapper;
	
	/**
	 * 新增分类
	 *
	 * @param categoryDTO
	 */
	@Override
	public void addCategory(CategoryDTO categoryDTO) {
		Category category = new Category();
		BeanUtils.copyProperties(categoryDTO, category);
		category.setStatus(StatusConstant.DISABLE);
		category.setCreateTime(LocalDateTime.now());
		category.setUpdateTime(LocalDateTime.now());
		category.setCreateUser(BaseContext.getCurrentId());
		category.setCreateUser(BaseContext.getCurrentId());
		Integer count = categoryMapper.getCategorySort(category.getSort());
		if (count > 0) {
			throw new AddDataException(MessageConstant.ADD_DATA_EXCEPTION);
		}
		categoryMapper.addCategory(category);
	}
	
	/**
	 * 修改分类
	 *
	 * @param categoryDTO
	 */
	@Override
	public void putCategory(CategoryDTO categoryDTO) {
		Category category = new Category();
		BeanUtils.copyProperties(categoryDTO, category);
		category.setUpdateTime(LocalDateTime.now());
		category.setCreateUser(BaseContext.getCurrentId());
		categoryMapper.putCategory(category);
	}
	
	/**
	 * 启用/禁用分类
	 *
	 * @param id
	 * @param status
	 */
	@Override
	public void disableCategory(Long id, Integer status) {
		Category category =
				Category.builder().id(id).status(status).updateTime(LocalDateTime.now()).updateUser(BaseContext.getCurrentId()).build();
		categoryMapper.putCategory(category);
	}
	
	/**
	 * 分页查询
	 *
	 * @param categoryPageQueryDTO
	 * @return
	 */
	@Override
	public PageResult getCategory(CategoryPageQueryDTO categoryPageQueryDTO) {
		// 插件自动计算分页
		PageHelper.startPage(categoryPageQueryDTO.getPage(), categoryPageQueryDTO.getPageSize());
		// 获取返回所有信息
		Page<Category> category = categoryMapper.getCategory(categoryPageQueryDTO);
		return new PageResult(category.getTotal(), category.getResult());
	}
	
	/**
	 * 根据id删除分类
	 *
	 * @param id
	 */
	@Override
	public void deleteCategory(Long id) {
		Category category = categoryMapper.getCategoryId(id);
		if (category.getType().equals(TypeConstant.VARIETY_OF_DISHES)) {
			Integer count = dishMapper.getDishCategory(id);
			if (count > 0) {
				throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_DISH);
			}
		} else {
			Integer count = setmealMapper.getSetmealCategory(id);
			if (count > 0) {
				throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_SETMEAL);
			}
		}
		categoryMapper.deleteCategory(id);
	}
	
	@Override
	public List<Category> getCategoryType(Integer type) {
		return categoryMapper.getCategoryType(type);
	}
	
	
}
